package com.qzq.weibo.activity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.adapter.CommentsAdapter;
import com.qzq.weibo.entity.Comment;
import com.qzq.weibo.http.HttpRequest;
import com.qzq.weibo.utils.Utils;
import com.qzq.weibo.view.LoadingDialog;

public class ReplyAndRetweetActivity extends Activity implements
		OnClickListener, TextWatcher {

	private ListView lv_comments;
	private Button btn_cancel;
	private Button btn_send;
	private TextView tv_title;
	private TextView tv_reply;
	private TextView tv_retweet;

	private View mHeader;
	private EditText header_et_content;
	private ImageView header_iv_emotion;
	private ImageView header_iv_delete;
	private TextView header_tv_text_num_left;

	private HttpRequest http;
	private Utils utils;
	private List<Comment> comments;
	private CommentsAdapter adapter;

	private int page = 1;
	private int count = 20;

	private int nor_color;
	private int warning_color;
	private Drawable tv_bg;

	private LoadingDialog dialog_send;
	private LoadingDialog dialog_loading;

	/** 评论内容 ||转发内容 */
	private String text;

	/**
	 * 动作响应， actionReponse ： reply:则是评论。 retweet:则是转发
	 * 
	 */
	private String actionReponse;

	/** 微博id */
	private String idstr;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reply_retweet);

		initView();
		initHeader();
		initMember();

	}

	/**
	 * 初始化布局
	 */
	private void initView() {

		lv_comments = (ListView) this.findViewById(R.id.reply_retweet_lv);
		mHeader = LayoutInflater.from(getApplicationContext()).inflate(
				R.layout.header_reply_retweet, null);
		btn_cancel = (Button) this
				.findViewById(R.id.reply_retweet_bar_btn_cancel);
		btn_send = (Button) this.findViewById(R.id.reply_retweet_bar_btn_send);
		tv_title = (TextView) this
				.findViewById(R.id.reply_retweet_bar_tv_title);
		tv_reply = (TextView) this.findViewById(R.id.reply_retweet_tv_reply);
		tv_retweet = (TextView) this
				.findViewById(R.id.reply_retweet_tv_retweet);

	}

	/**
	 * 初始化lv_comments头部布局
	 */
	private void initHeader() {

		header_et_content = (EditText) mHeader
				.findViewById(R.id.header_reply_retweet_et_content);
		header_iv_emotion = (ImageView) mHeader
				.findViewById(R.id.header_reply_retweet_iv_emotion);
		header_iv_delete = (ImageView) mHeader
				.findViewById(R.id.header_reply_retweet_iv_delete);
		header_tv_text_num_left = (TextView) mHeader
				.findViewById(R.id.header_reply_retweet_tv_text_num_left);

	}

	/**
	 * 初始化成员变量
	 */
	private void initMember() {

		actionReponse = getIntent().getStringExtra("actionReponse");
		idstr = getIntent().getStringExtra("idstr");

		nor_color = getResources().getColor(R.color.black);
		warning_color = getResources().getColor(R.color.red_lite);
		tv_bg = getResources().getDrawable(
				R.drawable.button_with_black_edge_alpha_normal);

		utils = Utils.getInstance(getApplicationContext());
		http = HttpRequest.getInstance(getApplicationContext());

		comments = new ArrayList<Comment>();
		lv_comments.addHeaderView(mHeader);
		adapter = new CommentsAdapter(getApplicationContext(),
				R.layout.list_item_restore, comments, idstr);

		lv_comments.setAdapter(adapter);
		/** 动态更新UI */
		changeUI();

		/** 绑定点击事件 */
		btn_cancel.setOnClickListener(this);
		btn_send.setOnClickListener(this);
		header_iv_delete.setOnClickListener(this);
		header_iv_emotion.setOnClickListener(this);

		/** 绑定输入监听 */
		header_et_content.addTextChangedListener(this);

	}

	/**
	 * 异步加载评论列表
	 */
	@SuppressLint("HandlerLeak")
	private void loading() {

		dialog_loading = new LoadingDialog(ReplyAndRetweetActivity.this,
				R.style.Theme_Transparent, "评论加载中...");
		dialog_loading.show();

		http.requestGetComments(new Handler() {

			@SuppressWarnings("unchecked")
			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);
				if (msg != null) {
					dialog_loading.dismiss();
					int flag = msg.what;
					if (flag == HttpRequest.REQUEST_FAILER) {
						HttpRequest.badRequest(
								Integer.parseInt(msg.obj.toString()),
								getApplicationContext());
					} else {
						comments.clear();
						comments.addAll((Collection<? extends Comment>) msg.obj);
						adapter.notifyDataSetChanged();

					}
				}
			}
		}, idstr, page, count);
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.reply_retweet_bar_btn_cancel:
			finish();
			break;

		case R.id.reply_retweet_bar_btn_send:
			onClickSend();
			break;

		case R.id.header_reply_retweet_iv_emotion:

			break;

		case R.id.header_reply_retweet_iv_delete:
			header_et_content.setText("");
			break;

		default:
			break;
		}
	}

	/**
	 * 发表评论/转发的实现
	 */
	@SuppressLint("HandlerLeak")
	private void onClickSend() {

		if (header_et_content.length() > 0) {
			if (header_et_content.length() <= 140) {

				text = header_et_content.getText().toString();

				dialog_send = new LoadingDialog(ReplyAndRetweetActivity.this,
						R.style.Theme_Transparent, "提交中...");
				dialog_send.show();
				/**
				 * 评论微博
				 * 
				 */
				if (actionReponse.equals("reply_status")) {
					http.requestReplyStatus(new Handler() {

						@Override
						public void handleMessage(Message msg) {
							if (msg != null) {
								dialog_send.dismiss();
								int flag = msg.what;
								if (flag == HttpRequest.REQUEST_FAILER) {
									HttpRequest.badRequest(
											Integer.valueOf(msg.obj.toString()),
											getApplicationContext());
								} else {
									utils.setToastMsg("评论成功");
									header_et_content.setText("");
									// try {
									// Thread.sleep(3*1000);
									// } catch (InterruptedException e) {
									// e.printStackTrace();
									// }
									loading();
								}
							}
							super.handleMessage(msg);
						}
					}, text, idstr);

					/**
					 * 转发微博
					 * 
					 */
				} else if (actionReponse.equals("retweet")) {

					http.requestRetweetStatus(new Handler() {
						@Override
						public void handleMessage(Message msg) {
							super.handleMessage(msg);
							if (msg != null) {
								dialog_send.dismiss();
								int flag = msg.what;

								if (flag == HttpRequest.REQUEST_FAILER) {
									HttpRequest.badRequest(
											Integer.valueOf(msg.obj.toString()),
											getApplicationContext());
								} else {
									utils.setToastMsg("转发成功");
									setResult(11);
									finish();
								}
							}
						}
					}, text, idstr);
				}

				else if (actionReponse.equals("reply_comment")) {
					String cid = getIntent().getStringExtra("cid");
					http.requestReplyComment(new Handler() {

						@Override
						public void handleMessage(Message msg) {
							super.handleMessage(msg);
							if (msg != null) {
								dialog_send.dismiss();
								int flag = msg.what;
								if (flag == HttpRequest.REQUEST_FAILER) {
									HttpRequest.badRequest(Integer
											.parseInt(msg.obj.toString()),
											getApplicationContext());
								} else {
									utils.setToastMsg("回复成功");
									header_et_content.setText("");
									finish();
								}
							}
						}
					}, text, idstr, cid);
				}

			} else {
				utils.setToastMsg("超出字数限制");
			}
		} else {
			utils.setToastMsg("内容不能为空");
		}
	}

	/**
	 * 根据不同的动作改变是否显示评论还是转发
	 * 
	 */
	private void changeUI() {
		if (actionReponse.equals("reply_status")) {

			tv_title.setText("评论微博");
			btn_send.setText("评论");
			tv_reply.setBackground(tv_bg);
			tv_retweet.setBackground(null);
			loading();

		} else if (actionReponse.equals("retweet")) {

			tv_title.setText("转发微博");
			btn_send.setText("转发");
			tv_reply.setBackground(null);
			tv_retweet.setBackground(tv_bg);
		} else if (actionReponse.equals("reply_comment")) {

			tv_title.setText("回复评论");
			btn_send.setText("回复");
			tv_reply.setBackground(tv_bg);
			header_et_content.setText("回复@"
					+ getIntent().getStringExtra("replyUserName") + ":");
			tv_retweet.setBackground(null);
		}
	}

	/**
	 * header_et_comment输入内容的动态监听
	 */
	@Override
	public void afterTextChanged(Editable s) {

	}

	@Override
	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {

	}

	@Override
	public void onTextChanged(CharSequence s, int start, int before, int count) {
		if (header_et_content.getText().length() <= 140) {
			header_tv_text_num_left.setTextColor(nor_color);
			header_tv_text_num_left.setText(String
					.valueOf((140 - header_et_content.getText().length())));
		} else {
			header_tv_text_num_left.setTextColor(warning_color);
			header_tv_text_num_left
					.setText("超出"
							+ String.valueOf((header_et_content.getText()
									.length() - 140)));
		}
	}
}
